﻿
using AutoMapper;
using D.Traceability.Domain;
using D.Traceability.ErrorCode;
using D.Traceability.Infrastructure.Infrastructure;
using D.Traceability.Write.API.APPlicant.Command;
using D.Traceability.Write.API.APPlicant.Command.TestCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.Traceability.Write.API.APPlicant.CommandHandler
{
    /// <summary>
    ///  添加企业信息-命令执行
    /// </summary>
    public class AddUpdateEnterpriseCommandHandler : IRequestHandler<AddUpdateEnterpriseCommand, APIResult<string>>
    {
        private readonly IBaseRepository<SU_EnterpriseModel> baseRepository;
        private readonly IMapper mapper;

        public AddUpdateEnterpriseCommandHandler(IBaseRepository<SU_EnterpriseModel> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }
        //////////////////////养殖企业信息////////////////////////////
        /// <summary>
        /// 添加/修改企业信息
        /// </summary>
        public async Task<APIResult<string>> Handle(AddUpdateEnterpriseCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();

            //////////////////////养殖企业信息////////////////////////////
            //查询是否含有数据(通过 生产批次 TraceaBatch查询)
            var obj = await baseRepository.GetAll().Result.FirstOrDefaultAsync(x => x.TraceaBatch == request.TraceaBatch && x.TraceaIsDelete == false);
            //////////////////////养殖企业信息////////////////////////////
            //有数据执行修改操作
            if (obj != null)
            {

                int res = 0; //修改次数
                //养殖企业信息
                if (request.BreedName != null)
                {
                    obj.BreedName = request.BreedName;              //养殖场
                    obj.BreedChargeName = request.BreedChargeName;  //负责人
                    obj.BreedNum = request.BreedNum;                //存栏数量
                                                                    //修改数据库
                    res = await baseRepository.Update(obj);
                }
                //屠宰企业信息
                if (request.SlaughterName != null)
                {
                    obj.SlaughterName = request.SlaughterName;              //公司名称
                    obj.SlaughterAdder = request.SlaughterAdder;             //公司地址
                    obj.SlaughterProvinceId = request.SlaughterProvinceId;      //省份 
                    obj.SlaughterCityId = request.SlaughterCityId;              //市
                    obj.SlaughterCountyId = request.SlaughterCountyId;          //县
                    obj.SlaughterCreteTime = request.SlaughterCreteTime;        //成立时间
                    obj.SlaughterBrief = request.SlaughterBrief;                //公司简介
                    //修改数据库
                    res = await baseRepository.Update(obj);
                }
                //深加工企业信息
                if (request.DeepName != null)
                {
                    obj.DeepName = request.DeepName;    //深加工公司名称
                    obj.DeepNumber = request.DeepNumber; //生产许可证号       
                    res = await baseRepository.Update(obj); //修改数据库
                }
                if (res > 0)
                {
                    result.Code = APIEnum.设置成功;
                    result.Msg = APIEnum.设置成功.ToString();
                    result.data = res.ToString();
                }
                else
                {
                    result.Code = APIEnum.设置失败;
                    result.Msg = APIEnum.设置失败.ToString();
                    result.data = res.ToString();
                }
            }
            else
            {
                //添加操作
                var model = mapper.Map<SU_EnterpriseModel>(request);
                int res = await baseRepository.Add(model);
                if (res > 0)
                {
                    result.Code = APIEnum.设置成功;
                    result.Msg = APIEnum.设置成功.ToString();
                    result.data = res.ToString();
                }
                else
                {
                    result.Code = APIEnum.设置失败;
                    result.Msg = APIEnum.设置失败.ToString();
                    result.data = res.ToString();
                }
            }
            return await Task.FromResult(result);
        }
    }
}
